package com.ruoyi.common.utils.poi;


import cn.hutool.system.OsInfo;
import cn.hutool.system.SystemUtil;
import com.aspose.words.*;
import lombok.SneakyThrows;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @author yonghaoRen
 * @Description:
 */
public class AsposeWordUtil {


    /**
     * word转pdf
     *
     * @param wordPath word文件保存的路径
     * @param pdfPath  转换后pdf文件保存的路径
     * @author yonghaoRen
     * @date 2020/12/25 13:51
     */
    @SneakyThrows
    public static void wordToPdf(String wordPath, String pdfPath) {
        getLicense();
        File file = new File(pdfPath);
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(file);
            OsInfo osInfo = SystemUtil.getOsInfo();
            if (osInfo.isLinux()) {
                //指定字体源
                FontSettings.getDefaultInstance().setFontsFolder("/usr/share/fonts", true);
            }
            Document doc = new Document(wordPath);
            doc.save(os, SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.flush();
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 加载license 用于破解 不生成水印
     *
     * @author yonghaoRen
     * @date 2022/12/5 13:51
     */
    public static void getLicense() throws Exception {
        try (InputStream is = AsposeWordUtil.class.getClassLoader().getResourceAsStream("WordLicense.xml")) {
            License license = new License();
            license.setLicense(is);
        }

    }

    public static void main(String[] args) throws Exception {
        System.out.println("Hello, AsPose Word!");
        wordToPdf("c:/工作量结算单.docx", "c:/工作量结算单.pdf");
    }

    /**
     * 渲染表格
     */
    public static void renderTableForListMap(Document doc, List<Map<String, String>> data, int tableIndex)
            throws Exception {
        // 在模板中查找表格
        Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
        Row templateRow = (Row) table.getLastRow().deepClone(true);

        // 移除模板行
        table.getLastRow().remove();

        // 插入数据行
        for (Map<String, String> record : data) {
            Row newRow = (Row) templateRow.deepClone(true);
            appendRow(newRow, record, table);
        }
    }

    /**
     * 追加一行
     */
    private static void appendRow(Row newRow, Map<String, String> record, Table table) throws Exception {
        for (Cell cell : newRow.getCells()) {
            for (Map.Entry<String, String> entry : record.entrySet()) {
                cell.getRange().replace("{{" + entry.getKey() + "}}", entry.getValue(), new FindReplaceOptions());
            }
        }
        table.appendChild(newRow);
    }

    /**
     * 渲染表格
     */
    public static void renderTableForListString(Document doc, List<List> value, int tableIndex) throws Exception {
        // 在模板中查找表格
        Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
        Row templateRow = (Row) table.getLastRow().deepClone(true);

        // 移除模板行
        table.getLastRow().remove();

        // 插入数据行
        int i = 0;
        for (List<String> record : value) {
            Row newRow = (Row) templateRow.deepClone(true);
            for (Cell cell : newRow.getCells()) {
                cell.getRange().replace("", record.get(i), new FindReplaceOptions());
            }
            table.appendChild(newRow);
            i++;
        }
    }
}